
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Edge()

try:
    driver.get("http://121.43.169.97:8848/pageA.html")
    driver.implicitly_wait(5)
    time.sleep(2)
    #   定位元素和输入内容
    username_input = driver.find_element(By.NAME, "userA")
    username_input.send_keys("admin")
    #   获取上面定位到的元素的大小
    size = username_input.size
    #   输出大小到项目运行窗口
    print(f"用户名输入框大小: {size}")
    #   定位第一个a标签
    first_link = driver.find_element(By.TAG_NAME, "a")
    #   获取第一个a标签的文本
    link_text = first_link.text
    print(f"第一个超链接文本: {link_text}")
    #   获取a标签的连接
    link_href = first_link.get_attribute("href")
    print(f"第一个超链接地址: {link_href}")

    try:
        #   定位元素和获取元素是否可见
        span_element = driver.find_element(By.TAG_NAME, "span")
        is_visible = span_element.is_displayed()
        print(f"span标签是否可见: {is_visible}")
    except:
        print("页面中未找到span标签")
    time.sleep(2)
    try:
        #   定位元素
        cancel_button = driver.find_element(By.XPATH, "/html/body/div/fieldset/form/p[5]/input")
        #   点击按钮
        cancel_button.click()
        #   获取按钮是否可用
        is_enabled = cancel_button.is_enabled()
        print(f"取消按钮是否可用: {is_enabled}")
    except:
        print("未找到'取消'按钮")

    try:
        #   双重定位复选框的旅游选项
        travel_checkbox = driver.find_element(By.XPATH, "//input[@type='checkbox' and @value='旅游']")
        #   获取旅游选项的选择情况
        is_selected = travel_checkbox.is_selected()
        print(f"'旅游' 复选框是否选中: {is_selected}")
    except:
        print("未找到 '旅游' 复选框")

finally:
    driver.quit()
